package com.github.zzay.news.controller;

import com.github.zzay.common.core.model.ResponseBean;
import com.github.zzay.news.dto.NewsDto;
import com.github.zzay.news.service.NewsService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author zzay
 * @className MainController
 * @description Main Controller
 * @create 2022/04/30 22:53
 */
@RestController
@DefaultProperties(defaultFallback = "mainGlobalFallback")
@Tag(name = "MainController", description = "主要页面跳转相关接口")
public class MainController {

    /**
     * News Service
     */
    @Autowired
    NewsService newsService;

    /**
     * Dashboard page
     *
     * @return Dashboard page
     */
    @Operation(summary = "主页面跳转", description = "主页面跳转",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @HystrixCommand(fallbackMethod = "dashboardPageFallback",
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")})
    @ApiResponse(responseCode = "200", description = "跳转到主页")
    @GetMapping({"/", "/index"})
    public ResponseBean dashboardPage(@Parameter(name = "newsDto") @RequestBody NewsDto newsDto) {
        HashMap<String, Object> data = new HashMap<>();
        // if no page number is declared, then display the first page by default
        Integer page = newsDto.getPage();
        if (page == null) {
            data.put("list", newsService.getListByPage(1));
            data.put("pageNum", 1);
        } else {
            data.put("list", newsService.getListByPage(page));
            data.put("pageNum", page);
        }
        // total page count of published news
        data.put("page", newsService.getPublishedPageCount());
        return new ResponseBean(HttpStatus.OK.value(), "Go to index page", data);
    }

    /**
     * Log-in page
     *
     * @return Log-in page
     */
    @Operation(summary = "登录页面跳转", description = "登录页面跳转",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @HystrixCommand(fallbackMethod = "loginPageFallback",
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")})
    @ApiResponse(responseCode = "200", description = "跳转到登录页面")
    @GetMapping("/login")
    public ResponseBean loginPage() {
        HashMap<String, Object> data = new HashMap<>();
        return new ResponseBean(HttpStatus.OK.value(), "Go to login page", data);
    }

    /**
     * Register page
     *
     * @return Register page
     */
    @Operation(summary = "注册页面跳转", description = "注册页面跳转",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @HystrixCommand(fallbackMethod = "registerPageFallback",
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")})
    @ApiResponse(responseCode = "200", description = "跳转到注册页面")
    @GetMapping("/register")
    public ResponseBean registerPage() {
        HashMap<String, Object> data = new HashMap<>();
        return new ResponseBean(HttpStatus.OK.value(), "Go to register page", data);
    }

    /**
     * Customized "unauthorized" error page
     *
     * @return Customized "unauthorized" error page
     */
    @Operation(summary = "未认证错误页面跳转", description = "未认证错误页面跳转",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponse(responseCode = "400", description = "当前为未认证状态，请登录认证")
    @GetMapping("/unauthorized")
    public ResponseBean accessDeniedPage() {
        HashMap<String, Object> data = new HashMap<>();
        return new ResponseBean(HttpStatus.BAD_REQUEST.value(), "Currently unauthorized, please authorize", data);
    }

}
